package com.bootdo.acitvity.DDD.domain.factory;

import com.bootdo.acitvity.DDD.domain.CenterBatch;
import com.bootdo.acitvity.DDD.domain.ChannelStock;
import com.bootdo.acitvity.DDD.domain.repository.mapper.SkuBatchChannelMapper;
import com.bootdo.acitvity.DDD.domain.repository.mapper.SkuBatchMapper;
import com.bootdo.acitvity.DDD.domain.vo.ChannelSkuBatch;
import com.bootdo.acitvity.DDD.domain.vo.SkuBatch;
import com.bootdo.acitvity.DDD.infrastrcture.convert.AssemblerConvert;
import com.bootdo.acitvity.DDD.infrastrcture.redis.RedisBatch;
import com.bootdo.acitvity.DDD.infrastrcture.redis.RedisBatchChannel;
import com.bootdo.acitvity.DDD.domain.entity.ChannelSoldAmount;
import com.bootdo.acitvity.DDD.domain.entity.ShareStock;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class StockFactory {
    @Resource
    SkuBatchMapper skuBatchMapper;

    @Resource
    private SkuBatchChannelMapper skuBatchChannelMapper;

    public RedisBatch getSkuBatch(String batchId) {
        SkuBatch skuBatch = skuBatchMapper.getProbablyByBatchId(batchId);
        AssemblerConvert assemblerConvert = new AssemblerConvert();
        RedisBatch redisBatch = assemblerConvert.convert((source, target) -> {
            target.setFbatchStatus(source.getFbatchStatus());
            target.setFskuId(source.getFskuId());
        }, skuBatch, RedisBatch.class);
        return redisBatch;
    }

    public List<RedisBatchChannel> getSkuChannelBatch(String batchId) {
        List<RedisBatchChannel> redisBatchChannels = new ArrayList<>();
        List<ChannelSkuBatch> channelSkuBatches = skuBatchChannelMapper.getSkuChannelBatch(batchId);
        AssemblerConvert assemblerConvert = new AssemblerConvert();
        for (ChannelSkuBatch channelSkuBatch : channelSkuBatches) {
            RedisBatchChannel redisBatchChannel = assemblerConvert.convert((source, target) -> {
                target.setFbatchStatus(source.getFbatchStatus());
                target.setFskuId(source.getFskuId());
                target.setFchannelId(source.getFchannelId());
            }, channelSkuBatch, RedisBatchChannel.class);
            redisBatchChannels.add(redisBatchChannel);
        }
        return redisBatchChannels;
    }

    public CenterBatch buildCenterBatch(RedisBatch redisBatch) {
        CenterBatch centerBatch = new CenterBatch(redisBatch.getFskuBatchId(), redisBatch.getFsupplierSkuBatchId());
        centerBatch.setBatchValidateStartDate(redisBatch.getFvalidityStartDate());
        centerBatch.setBatchValidateEndDate(redisBatch.getFvalidityEndDate());
        centerBatch.setShareStock(new ShareStock(redisBatch.getFstockChannelNum()));
        for (RedisBatchChannel batchChannel : redisBatch.getList()) {
            centerBatch.assignChannel(batchChannel.getFchannelId());
            ChannelStock channelStock = centerBatch.getChannelStockMap().get(batchChannel.getFchannelId());
            channelStock.setChannelSoldAmounts(new ChannelSoldAmount(batchChannel.getFsellNum()));
            channelStock.setLastLockRestoredTime(batchChannel.getFlastInitTime());
        }
        return centerBatch;
    }

    public SkuBatch buildSkuBatch(CenterBatch centerBatch) {
        SkuBatch skuBatch = new SkuBatch();
        skuBatch.setFskuBatchId(centerBatch.getId());
        skuBatch.setFstockRemianNum(centerBatch.getShareStock().getStockAmount());
        return skuBatch;
    }

    public List<ChannelSkuBatch> buildSkuBatchChannel(CenterBatch centerBatch) {
        List<ChannelSkuBatch> channelSkuBatches = new ArrayList<>();
        Map<Long, ChannelStock> channelStockMap = centerBatch.getChannelStockMap();
        channelStockMap.forEach((k,v)->{
            ChannelSkuBatch channelSkuBatch = new ChannelSkuBatch();
            channelSkuBatch.setFbatchChannelId(k);
            channelSkuBatch.setFlockStockNum(v.getChannelLockedStock().getStockAmount());
        });

        return channelSkuBatches;
    }
}
